#!/bin/bash

set -x

# default to update source then build

TOP_DIR=$(pwd)
SRC_DIR=$TOP_DIR/lib_source
BD_DIR=$TOP_DIR/lib_build
BIN_DIR=$TOP_DIR/lib_bin

VER_OPT=5.0.5

export PATH=$TOP_DIR/cross/bin:$PATH

cmd=all64
if [ -n "$1" ] ; then
	cmd=$1
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "update" ] ; then
	mkdir -p $SRC_DIR || exit 0
	cd $SRC_DIR
	if [ ! -d giflib-$VER_OPT ] ; then
		wget http://sourceforge.net/projects/giflib/files/giflib-5.x/giflib-$VER_OPT.tar.bz2/download -O giflib-$VER_OPT.tar.bz2
		tar xjf giflib-$VER_OPT.tar.bz2
	fi
	cd ..
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "b64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
	HOST_FLAG="--host=x86_64-w64-mingw32"
	if [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
		HOST_FLAG="--host=i686-w64-mingw32"
	fi
	rm -rf $BIN_DIR/giflib
	mkdir -p $BD_DIR
	cd $BD_DIR
	rm -rf bd_giflib
	mkdir bd_giflib
	cd bd_giflib
	patch -d $SRC_DIR/giflib-$VER_OPT -p0 < $TOP_DIR/patch/giflib.patch
	$SRC_DIR/giflib-$VER_OPT/configure $HOST_FLAG --prefix=$BIN_DIR/giflib \
		--disable-shared --enable-static

#	--enable-static --disable-shared
#	--enable-shared --disable-static
	make $MAKE_OPT
	make install
	patch -d $SRC_DIR/giflib-$VER_OPT -p0 -R < $TOP_DIR/patch/giflib.patch
	cd ..

	cd ..
fi

